package com.scedu.controller;

import com.scedu.dto.OrderDTO;
import com.scedu.enums.ResultEnums;
import com.scedu.exception.SellException;
import com.scedu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/*
卖家端订单
 */
@Slf4j
@Controller
@RequestMapping("/seller/order")
public class SellerOrderController {
    @Autowired
    private OrderService orderService;
    /*
    订单列表
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageRequest pageRequest= PageRequest.of(page-1,size);
        Page<OrderDTO> orderDTOPage= orderService.findList(pageRequest);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("orderDTOPage",orderDTOPage);
        return  new ModelAndView("order/list",map);
    }
    /*
    取消订单
     */
    @GetMapping("/cancel")
    public ModelAndView cancel (@RequestParam("orderId") String orderId,
                                Map<String,Object> map){
        try{
            OrderDTO orderDTO=orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }
        catch ( SellException e){
            log.error("订单不存在,orderId={}",orderId);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }



        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    /*
    订单详情
     */
    @GetMapping("/detail")
    public ModelAndView detail (@RequestParam("orderId") String orderId,
                                Map<String,Object> map){
        try{
            OrderDTO orderDTO=orderService.findOne(orderId);
            map.put("orderDTO",orderDTO);
            return new ModelAndView("order/detail",map);

        }
        catch ( SellException e){
            log.error("订单不存在,orderId={}",orderId);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
    }
    /*
   完结订单
    */
    @GetMapping("/finish")
    public ModelAndView finish (@RequestParam("orderId") String orderId,
                                Map<String,Object> map){
        try{
            OrderDTO orderDTO=orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }
        catch ( SellException e){
            log.error("订单不存在,orderId={}",orderId);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }



        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

}
